package org.itboys.core.controller.web.company;

import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.service.mongo.web.SundayStoreService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/3
 * @Description：仓库
 */
@Controller
@RequestMapping("/sunday/web/company/store")
public class SundayCompanyStoreController extends BaseController {
    @Autowired
    private SundayStoreService storeService;
    @Autowired
    private SystemUserService userService;
    @Autowired
    private SundayMemberService memberService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, Model model) {
        return "/sunday/company/store/index";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        Long userId = AdminSessionHolder.getAdminUserId();
        SystemUser user = userService.getById(userId);
        PageResult<SundayStore> result = storeService.select(request, user);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    @RequestMapping("/input")
    public String input(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "id", required = true) Long id, Model model) {

        SundayStore store = new SundayStore();
        if (id == 0) {
            store.setId(0L);
        } else {
            store = storeService.getById(id);
        }
        model.addAttribute("store", store);
        return "/sunday/company/store/input";
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayStore store,
                     @RequestParam(value = "imageFile", required = false) MultipartFile imageFile) {
        Long userId = AdminSessionHolder.getAdminUserId();
        SundayMember member = memberService.getMemberByUserId(userId);
        storeService.save(member,store, imageFile);
        ResponseMessage.success();

    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       @RequestParam(value = "id", required = true) Long id) {
        storeService.delete(id);
        ResponseMessage.success();
    }

    @RequestMapping("/choose")
    public String choose(HttpServletRequest request,
                         @RequestParam(value = "isSingle", required = true) Integer isSingle,//是否单选
                         @RequestParam(value = "callBack", required = false) String callBack,//回调参数
                         @RequestParam(value = "param", required = false) String param,
                         Model model) {
        model.addAttribute("param", param);
        model.addAttribute("callBack", callBack);
        if (isSingle == null) {
            return "/sunday/company/store/choose/choose_erro";
        } else if (isSingle == 1) {
            return "/sunday/company/store/choose/choose_single";
        } else if (isSingle == 0) {
            return "/sunday/company/store/choose_muilt";
        }
        return "/finance/store/choose/choose_erro";
    }
}
